package com.example.download.widget.loading

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import com.example.download.R


/**
 * Created by  you
 * Date: 2022/3/24
 * Time: 16:30
 */
class DownLoadControllor : FrameLayout {
    private var tvCancel: TextView? = null
    private var progressView: CircularProgressView? = null
    private var tvProgress: TextView? = null
    private var imageComplete: ImageView? = null
    private var mCancelListener: (() -> Unit)? = null

    constructor(context: Context?, attrs: AttributeSet?) : super(context!!, attrs) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context!!,
        attrs,
        defStyleAttr
    ) {
        init()
    }

    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int
    ) : super(
        context!!, attrs, defStyleAttr
    ) {
        init()
    }

    private fun init() {
        LayoutInflater.from(context).inflate(R.layout.minivideo_download_controllor, this)
        tvCancel = findViewById<TextView>(R.id.tv_cancel_controllor)
        progressView = findViewById<CircularProgressView>(R.id.progress_download_controllor)
        tvProgress = findViewById<TextView>(R.id.tv_progress_controllor)
        imageComplete =  findViewById<ImageView>(R.id.image_download_complete)
        tvCancel?.setOnClickListener {
            mCancelListener?.invoke()
        }
    }

    /**
     * 设置下载进度
     */
    @SuppressLint("SetTextI18n")
    fun setProgressBar(progress: Int, size: String) {
        progressView?.progress = progress
        tvProgress?.text = if (progress < 100) "下载中(${size}MB/$progress%)" else "下载完成"

    }

    /**
     * 取消下载
     */
    fun setCancelListener(cancelListener: (() -> Unit)) {
        this.mCancelListener = cancelListener
    }

    /**
     *  设置取消按钮是否显示
     */
    fun setDownLoadCompleted() {
        tvCancel?.visibility =View.GONE
        imageComplete?.visibility = View.VISIBLE

    }

    fun getCancelButtonVisible() = tvCancel?.visibility
}

